package com.swt.gap.controller;

import com.swt.gap.common.PageResult;
import com.swt.gap.model.vo.*;
import com.swt.gap.model.vo.fileclass.UserFileClassVO;
import com.swt.gap.model.vo.resourcecenter.ResourceCenterFileFormVO;
import com.swt.gap.model.vo.resourcecenter.ResourceSearchResultVO;
import com.swt.gap.service.FileClassService;
import com.swt.gap.service.FileService;
import com.swt.gap.service.ResourceService;
import com.swt.gap.util.UserLoginUtils;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zqw
 * @date 2020/5/12 14:51
 */

@Api(tags = "200. 资源管理页面")
@RestController
@RequestMapping("/resource")
@Slf4j
public class ResourceController {

    private final ResourceService resourceService;
    private final FileService fileService;
    private final FileClassService fileClassService;

    public ResourceController(ResourceService resourceService, FileService fileService, FileClassService fileClassService) {
        this.resourceService = resourceService;
        this.fileService = fileService;
        this.fileClassService = fileClassService;
    }

//    @GetMapping("/index")
//    @ApiOperation(value = "资源中心首页", notes = "资源中心首页")
//    @ApiResponses({
//            @ApiResponse(code = 200, message = "ok", response = ResourceCenterFileFormVO.class),
//            @ApiResponse(code = 500, message = "失败")
//    })
//    public ResponseEntity getIndexData(){
//
//        int userId = UserLoginUtils.getUserId();
//        List<ResourceCenterFileFormVO> resourceCenterFileFormVOList = resourceService.getIndexData(userId);
//        if (resourceCenterFileFormVOList == null){
//            return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body("获取资源中心首页数据失败");
//        }
//
//        return ResponseEntity.ok(resourceCenterFileFormVOList);
//    }

   /* @GetMapping("/count-number")
    @ApiOperation(value = "统计内部资源和外部资源数量", notes = "统计内部资源和外部资源数量")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = ResourceNumber.class),
            @ApiResponse(code = 500, message = "失败")
    })
    public ResponseEntity countNumber(){
        int userId = UserLoginUtils.getUserId();
        List<ResourceNumber> resourceNumbers = resourceService.countNumber(userId);

        if(resourceNumbers == null){
            return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body("获取统计数量失败");
        }

        return ResponseEntity.ok(resourceNumbers);

    }*/
    @GetMapping("/search")
    @ApiOperation(value = "资源中心搜索", notes = "资源中心搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "location", dataType = "int",paramType = "query", value = "搜索位置 1：资源中心首页搜索 2：资源中心内部数据管理类别内搜索",required = true),
            @ApiImplicitParam(name = "fileClassId", dataType = "int",paramType = "query", value = "资源类别id,location为2时才需填"),
            @ApiImplicitParam(name = "keyword", dataType = "String",paramType = "query", value = "关键字", required = true),
            @ApiImplicitParam(name = "orderType", dataType = "int",paramType = "query", value = "排序方式 1：时间降序 2：时间升序 3：名称降序 4：名称升序"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", dataType = "int", value = "当前页"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "int", value = "每页的条数")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = ResourceCenterFileFormVO.class, responseContainer = "List"),
            @ApiResponse(code = 500, message = "失败")
    })
    public ResponseEntity searchResource( @RequestParam Integer location,
                                          @RequestParam("fileClassId") Integer fileClassId,
                                          @RequestParam("keyword") String keyword,
                                          @RequestParam(value = "orderType",defaultValue = "1") Integer orderType,
                                          @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                          @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){

        int userId = UserLoginUtils.getUserId();
        if (location==1 && StringUtils.isBlank(keyword)) {
            List<ResourceCenterFileFormVO> resourceCenterFileFormVOList = resourceService.getIndexData(userId);
            if (resourceCenterFileFormVOList == null){
                return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body("获取资源中心首页数据失败");
            }
            return ResponseEntity.ok(resourceCenterFileFormVOList);
        }else {
            PageResult<ResourceSearchResultVO> pageResult = resourceService.searchResource(location, userId, fileClassId, keyword, orderType, pageNum, pageSize);

            if (pageResult == null) {
                return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body("搜索资源失败");
            }

            return ResponseEntity.ok(pageResult);
        }
    }

//    @GetMapping("/search")
//    @ApiOperation(value = "资源中心搜索", notes = "资源中心搜索")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "location", dataType = "int",paramType = "query", value = "搜索位置 1：资源中心首页搜索 2：资源中心内部数据管理类别内搜索",required = true),
//            @ApiImplicitParam(name = "fileClassId", dataType = "int",paramType = "query", value = "资源类别id,location为2时才需填"),
//            @ApiImplicitParam(name = "keyword", dataType = "int",paramType = "query", value = "关键字"),
//            @ApiImplicitParam(name = "orderType", dataType = "int",paramType = "query", value = "排序方式 1：时间降序 2：时间升序 3：名称降序 4：名称升序"),
//            @ApiImplicitParam(paramType = "query", name = "pageNum", dataType = "int", value = "当前页"),
//            @ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "int", value = "每页的条数")
//    })
//    @ApiResponses({
//            @ApiResponse(code = 200, message = "ok"),
//            @ApiResponse(code = 500, message = "失败")
//    })
//    public ResponseEntity searchResource( @RequestParam(required = true) Integer location,
//                                         @RequestParam("fileClassId") Integer fileClassId,
//                                         @RequestParam(required = false) String keyword,
//                                         @RequestParam(value = "orderType",defaultValue = "1") Integer orderType,
//                                         @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
//                                         @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
//
//        int userId = UserLoginUtils.getUserId();
//        PageResult<ResourceSearchResultVO> pageResult = resourceService.searchResource(location,userId,fileClassId,keyword,orderType,pageNum,pageSize);
//
//        if (pageResult == null){
//            return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body("搜索资源失败");
//        }
//
//        return ResponseEntity.ok(pageResult);
//
//    }

    @GetMapping("/statistics")
    @ApiOperation(value = "数据统计",notes = "数据统计")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "query", name = "pageNum", dataType = "int", value = "当前页"),
        @ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "int", value = "每页的条数")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = UserFileClassVO.class, responseContainer = "List")})
    public ResponseEntity<PageResult<UserFileClassVO>> statistics(@RequestParam(value = "pageNum", required = false)Integer pageNum,
                                     @RequestParam(value = "pageSize", required = false) Integer pageSize){
        return ResponseEntity.ok(resourceService.dataStatistics(pageNum, pageSize));
    }


    @PostMapping("move")
    @ApiOperation(value = "数据移动分组-步骤1",notes = "数据移动分组")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "moveFile", dataType = "MoveFile", value = "MoveFile")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = MoveFileVO.class)})
    public ResponseEntity moveFile(@RequestBody MoveFile moveFile){
        if (moveFile.getFiles().isEmpty() ||moveFile.getFileClassId()==null){
            return ResponseEntity.status(400).body("参数不正确");
        }

        List<MoveRepeatFileVO> back= resourceService.moveFile(moveFile);
        String className =  fileClassService.selectFileClassById(moveFile.getFileClassId()).getClassName();
        MoveFileVO moveFileVO = new MoveFileVO();

        if (moveFile.getFiles().size()==1&& back.size()==1){
            String fileName =   fileService.getFileByid(moveFile.getFiles().get(0)).getOriginalFileName();
            moveFileVO.setMessage("“"+className+"”中已包含“"+fileName+"”请选择你的操作：");
            moveFileVO.setClassName(className);
            moveFileVO.setNum(back.size());
            moveFileVO.setFiles(back);
            return ResponseEntity.status(400).body(moveFileVO);
        }
        if (back.isEmpty()){
            return ResponseEntity.ok("移动成功");
        }else {
            moveFileVO.setMessage("“"+className+"”中包含“"+back.size()+"”个同名数据源,请选择你的操作");
            moveFileVO.setClassName(className);
            moveFileVO.setNum(back.size());
            moveFileVO.setFiles(back);
            return ResponseEntity.status(400).body(moveFileVO);
        }


//        String className =  fileClassService.selectFileClassById(moveFile.getFileClassId()).getClassName();
//        MoveFileVO moveFileVO = new MoveFileVO();
//
//        if (moveFile.getFiles().size()==1&& back.size()==1){
//            String fileName =   fileService.getFileByid(back.get(0)).getFileName();
//            moveFileVO.setMessage("“"+className+"”"+"中已包含“"+fileName+"”请选择你的操作：");
//            moveFileVO.setClassName(className);
//            moveFileVO.setNum(back.size());
//            moveFileVO.setFiles(back);
//          return ResponseEntity.status(400).body(moveFileVO);
//        }
//        if (back.size() == 0){
//            return ResponseEntity.ok("移动成功");
//        }else {
//            moveFileVO.setMessage(className+"中包含"+back.size()+"个同名数据源,请选择你的操作");
//            moveFileVO.setClassName(className);
//            moveFileVO.setNum(back.size());
//            moveFileVO.setFiles(back);
//            return ResponseEntity.status(400).body(moveFileVO);
//        }
//        return ResponseEntity.ok(back);
    }

    @PostMapping("over-or-keep")
    @ApiOperation(value = "数据移动分组-步骤2",notes = "数据移动分组")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "operator", dataType = "int", value = "1:直接覆盖，2：全部保留"),
            @ApiImplicitParam(paramType = "body", name = "moveFile", dataType = "MoveFile", value = "MoveFile")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = MoveFileVO.class)})
    public ResponseEntity moveFileOperator(@RequestParam Integer operator,
                                           @RequestBody MoveFile moveFile) {

        return ResponseEntity.ok(resourceService.forcedMoveFile(operator, moveFile));
    }

    /*@PostMapping("reName")
    @ApiOperation(value = "数据源名称编辑",notes = "数据源名称编辑")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "fileName", value = "数据名称"),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "fileId", value = "数据Id")
    })
    @ApiResponses({@ApiResponse(code = 201, message = "CREATED", response = UploadFileVO.class),
            @ApiResponse(code = 409, message = "已重名")})
    public ResponseEntity uploadRepeatFile(@RequestParam String fileName, @RequestParam Integer fileId) {


        UploadFileVO uploadFileVO = resourceService.reName(fileName, fileId);


        if(!uploadFileVO.getUploadSuccess()){
            return ResponseEntity.status(HttpStatus.CONFLICT).body(uploadFileVO);
        }
        return ResponseEntity.status(HttpStatus.CREATED).body(uploadFileVO);
    }*/




}
